package cn.tianwt.proxy.http.server;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

class PipeThread implements Runnable {
	private final Socket client;
	private final Socket server;
	private final InputStream is;
	private final OutputStream os;
	
	public PipeThread(Socket client, Socket server, InputStream is, OutputStream os) {
		this.client = client;
		this.server = server;
		this.is = is;
		this.os = os;
	}
	
	@Override
	public void run() {
		try {
			byte[] data = new byte[102400];
			int length = -1;
			while (!client.isInputShutdown()) {
				if ((length = is.read(data)) != -1) {
					os.write(data,0,length);
					os.flush();
				} else {
					//client端已经关闭了输出流.
					server.shutdownOutput();
					client.shutdownInput();
				}
			}
		} catch (Exception e) {
		}
	}
}
